package com.telecom.cms.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.telecom.cms.dto.CampCampProm;
import com.telecom.cms.util.Constant;
import com.telecom.cms.util.CurrentTime;

public class CampCampPromDAO {
	Dao dao = new DaoImp();

	String db = (new Constant()).getPara("DATABASE");

	public int insert(CampCampProm transientInstance) {
		int rs = 0;
		String sql = "select * from  " + db + ".Camp_Camp_Prom where Camp_Id="
				+ transientInstance.getCampId() + " and prom_id="
				+ transientInstance.getPromId();
		try {
			ResultSet rs2 = dao.select(sql);
			if (rs2.next())
				sql = "update " + db + ".Camp_Camp_Prom set latn_id='"
						+ transientInstance.getLatnId() + "',Creator_Name='"
						+ transientInstance.getCreatorName()
						+ "',Create_Time=timestamp '"
						+ CurrentTime.getCurrentTime() + "'where Camp_Id="
						+ transientInstance.getCampId() + " and prom_id="
						+ transientInstance.getPromId();
			else
				sql = "insert into " + db + ".Camp_Camp_Prom values( '"
						+ transientInstance.getCampId() + "', '"
						+ transientInstance.getPromId() + "' , '"
						+ transientInstance.getLatnId() + "' , '"
						+ transientInstance.getCreatorName() + "' ,  "
						+ "timestamp '" + CurrentTime.getCurrentTime() + "')";
			rs = dao.insert(sql);
			if (rs2 != null) {

				rs2.close();
			}

		} catch (Exception re) {
			System.out.println("save failed:" + re);
		}
		finally
		{
			dao.closeconn();
		}
		return rs;

	}

	public void delete(CampCampProm persistentInstance) {

		String sql = "delete from " + db + ".Camp_Camp_Prom where Camp_Id = "
				+ persistentInstance.getCampId();

		try {
			dao.delete(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public CampCampProm findById(String id) {

		String sql = "select * from " + db + ".Camp_Camp_Prom where Camp_Id = "
				+ id;
		CampCampProm sd = new CampCampProm();
		try {
			ResultSet rs = dao.select(sql);
			if (rs.next()) {
				sd.setCampId(rs.getInt(1));
				sd.setPromId(rs.getInt(2));
				sd.setLatnId(rs.getString(3));
				sd.setCreatorName(rs.getString(4));
				sd.setCreateTime(rs.getString(5));
			}
			if (rs != null) {

				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}
		return sd;

	}

	public List findAll() {

		String sql = "select * from " + db + ".Camp_Camp_Prom";
		List list = new ArrayList();
		try {
			ResultSet rs = dao.select(sql);
			while (rs.next()) {
				CampCampProm sd = new CampCampProm();
				sd.setCampId(rs.getInt(1));
				sd.setPromId(rs.getInt(2));
				sd.setLatnId(rs.getString(3));
				sd.setCreatorName(rs.getString(4));
				sd.setCreateTime(rs.getString(5));
				list.add(sd);
			}
			if (rs != null) {

				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}
		return list;

	}

	public List findAll(String campid) {

		String sql = "select * from " + db + ".Camp_Camp_Prom where camp_id="
				+ campid;
		List list = new ArrayList();
		try {
			ResultSet rs = dao.select(sql);
			while (rs.next()) {
				CampCampProm sd = new CampCampProm();
				sd.setCampId(rs.getInt(1));
				sd.setPromId(rs.getInt(2));
				sd.setLatnId(rs.getString(3));
				sd.setCreatorName(rs.getString(4));
				sd.setCreateTime(rs.getString(5));
				list.add(sd);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}
		return list;

	}

}
